package luaj.utils.functions;

import android.ext.ThreadManager;

import luaj.LuaValue;
import luaj.Varargs;
import luaj.lib.VarArgFunction;
import luaj.utils.LuaActivity;

public class runThread extends VarArgFunction {

    public Varargs invoke(Varargs args) {
        LuaValue f = args.arg1();
        int n = args.narg() - 1;
        LuaValue[] as = new LuaValue[n];
        for (int i = 0; i < n; i++) {
            as[i] = args.arg(i + 2);
        }
        ThreadManager.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    f.invoke(as);
                } catch (Exception e) {
                    e.printStackTrace();
                    LuaActivity.sendMsg(e.toString());
                }
            }
        });
        return LuaValue.NIL;
    }
}